package com.ruoyi.web.controller.shopping.backend;

import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.shopping.Product;
import com.ruoyi.system.domain.shopping.dto.ProductVoDto;
import com.ruoyi.system.domain.shopping.vo.ProductVo;
import com.ruoyi.system.service.shopping.ProductService;
import com.ruoyi.system.view.common.Result;
import com.ruoyi.system.view.common.ResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/backend/shopping/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    public ResultList<List<ProductVo>> list(Product product){
        startPage();
        List<ProductVo> productVos = productService.selectProductList(product);
        PageInfo pageInfo = new PageInfo(productVos);
        return ResultList.success(productVos, (int) pageInfo.getTotal(),"查询成功");
    }
    @PostMapping
    public Result add(@RequestBody Product product) {
        Boolean add = productService.add(product);
        if(add){
            return Result.success(null,"新增成功");
        }
        return Result.error(null,201,"新增失败");
    }
    @GetMapping("/{productId}")
    public Result<ProductVo> getInfo(@PathVariable Integer productId){
        ProductVo productVo = productService.selectProductVoById(productId);
        return Result.success(productVo,"查询成功");
    }
    @PutMapping
    public Result update(@RequestBody Product product){
        Boolean aBoolean = productService.update(product);
        if(aBoolean){
            return Result.success(null,"更新成功");
        }
        return Result.error(null,201,"更新失败");
    }
    @DeleteMapping("/{productId}")
    public Result delete(@PathVariable Integer[] productId){
        Boolean aBoolean = productService.deleteByIds(productId);
        if(aBoolean){
            return Result.success(null,"操作成功");
        }
        return Result.error(null,201,"操作失败");
    }
}
